package com.best.controller;

import com.best.dto.FoodWithComment;
import com.best.pojo.Food;
import com.best.pojo.FoodComment;
import com.best.result.Response;
import com.best.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 美味作品集
 *
 * @author yangruyi
 * @date 2021/3/11
 */
@RestController
@RequestMapping("/food/")
public class FoodController {

    @Autowired
    private FoodService foodService;

    /**
     * 制作
     *
     * @param food FoodDto
     * @return Response
     */
    @PostMapping("make")
    public Response makeFood(@RequestBody @Validated Food food){
        boolean b = foodService.addFood(food);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 删除
     *
     * @param id 作品id
     * @return Response
     */
    @DeleteMapping("{id}")
    public Response delFood(@PathVariable String id){
        boolean del = foodService.del(id);
        if(!del){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 修改
     *
     * @param food Food
     * @return Response
     */
    @PutMapping("edit")
    public Response editFood(@RequestBody Food food){
        boolean edit = foodService.edit(food);
        if(!edit){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 列表 可按名字模糊搜索，也可以加条件
     *
     * @param name 作品名
     * @param series 菜系
     * @param snack 小吃？
     * @param taste 口味
     * @param effect 功效
     * @param userId 用户id 个人作品集
     * @return Response
     */
    @GetMapping()
    public Response getAll(@RequestParam(required = false) String name,
                           @RequestParam(required = false)Integer series,
                           @RequestParam(required = false)Integer snack,
                           @RequestParam(required = false)Integer taste,
                           @RequestParam(required = false) Integer effect,
                           @RequestParam(required = false) String userId){
        List<Food> all = foodService.all(name,series,snack,taste,effect,userId);
        return Response.ok().data("all",all);
    }

    /**
     * 作品详情
     *
     * @param foodId 作品id
     * @return FoodWithComment
     */
    @GetMapping("/{foodId}")
    public FoodWithComment foodDetail(@PathVariable String foodId){
        return foodService.foodDetail(foodId);
    }
    /**
     * 点赞
     *
     * @param id 作品id
     * @return Response
     */
    @PutMapping("love/{id}")
    public Response easyLove(@PathVariable String id){
        boolean love = foodService.love(id);
        if(!love){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 评论列表，某个作品下有那些评论
     *
     * @param id 作品id
     * @return List
     */
    @GetMapping("/comment/{id}")
    public List<FoodComment> comments(@PathVariable String id){
       return foodService.comments(id);
    }

    /**
     * 评论某个作品
     *
     * @param comment FoodComment
     * @return Response
     */
    @PostMapping("say")
    public Response addComment(@RequestBody FoodComment comment){
        boolean b = foodService.addComment(comment);
        if(!b) {
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 收藏某个作品
     *
     * @param foodId 作品id
     * @param userId 用户id
     * @return Response
     */
    @PostMapping("love/{foodId}/{userId}")
    public Response loveFood(@PathVariable String foodId,@PathVariable String userId){
        boolean b = foodService.addCollect(foodId, userId);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 取消收藏某个作品
     *
     * @param foodId 作品id
     * @param userId 用户id
     * @return Response
     */
    @DeleteMapping("love/{foodId}/{userId}")
    public Response deLoveFood(@PathVariable String foodId,@PathVariable String userId){
        boolean b = foodService.subCollect(foodId, userId);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }
    /**
     * 上传文件
     *
     * @param file 文件
     * @return String
     */
    @PostMapping("/upload")
    public String singleFileUpload(@RequestBody MultipartFile file) {
        return foodService.uploadImage(file);
    }

    /**
     * 读取文件
     *
     * @param link 文件链接
     * @param response HttpServletResponse
     */
    @GetMapping("/media")
    public void getImage(String link, HttpServletResponse response){
        foodService.getImage(link,response);
    }
}
